import {
  queryGameList,
  addGame,
  modifyGame,
  changeStatus,
  findById,
  findProjectDict,
  findGameProject,
  addGameProject,
  removeGameProject,
  addZubie,
  modifyZubie,
  closeGameProjectZubie,
  openProjectZubie,
  removeGameRole,
} from '../services/gameList';
import { routerRedux } from 'dva/router';
import { message } from 'antd';
import { startsWith } from 'lodash';

export default {
  namespace: 'gameList',

  state: {
    gameData: {},
    submitting: false,
    addStatus: undefined,
    modifyItem: {},
  },

  effects: {
    *fetch({ payload }, { select, call, put }) {
      const response = yield call(queryGameList, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: { gameData: response },
        });
      }
    },
    *findById({ payload, callback }, { select, call, put }) {
      const response = yield call(findById, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: { modifyItem: response },
        });
        if (callback) callback();
      }
    },
    *addGame({ payload, callback }, { select, call, put }) {
      const response = yield call(addGame, payload);
      if (response) {
        if (callback) callback();
      }
    },
    *modifyGame({ payload }, { select, call, put }) {
      const response = yield call(modifyGame, payload);
      if (response) {
        yield put({
          type: 'changeState',
          payload: { modifyItem: response },
        });
        message.success('赛事修改成功！');
      }
    },
    *removeGameRole({ payload, callback }, { select, call, put }) {
      yield call(removeGameRole, payload);
      if (callback) callback();
    },

    *changeStatus({ payload }, { select, call, put }) {
      yield call(changeStatus, payload);
      yield put({
        type: 'fetch',
        payload: {
          page_size: 20,
          current_page: 1,
        },
      });
    },
  },

  reducers: {
    changeState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
